﻿@inject IUserBasketService basketService
@inject IUserService userService
@inject IUserStatusService userStatusService

<MudDialog>
    <DialogContent>
        <MudSelect T=" Basket " Label="Destination Basket" HelperText="Select destination basket for the current basket items" @bind-Value="selectedBasket" Variant="Variant.Filled">
            @foreach( var basket in allBaskets )
            {
                <MudSelectItem T=" Basket " Value="@basket">@basket.Name</MudSelectItem>
            }
        </MudSelect>
        @if( !string.IsNullOrEmpty(errorMsg) )
        {
            <p>@errorMsg</p>
        }
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="Copy">Copy</MudButton>
        <MudButton Color="Color.Primary" OnClick="Move">Move</MudButton>
    </DialogActions>
</MudDialog>

@code {

    [CascadingParameter] MudDialogInstance MudDialog { get; set; }

    readonly List<Basket> allBaskets = new();

    private Basket selectedBasket;

    async Task Move()
    {
        // TODO: This can be done in one step via the API
        await basketService.CopyImages(selectedBasket.BasketId);
        await basketService.Clear();

        MudDialog.Close(DialogResult.Ok(true));
    }

    async Task Copy()
    {
        await basketService.CopyImages(selectedBasket.BasketId);
        MudDialog.Close(DialogResult.Ok(true));
    }

    private string errorMsg;

    private void DisplayError(string errorText)
    {
        errorMsg = errorText;
        StateHasChanged();
    }

    void Cancel()
    {
        MudDialog.Cancel();
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var baskets = await basketService.GetUserBaskets(userService.UserId);
        var destBaskets = baskets.Where(x => x.BasketId != basketService.CurrentBasket.BasketId)
            .ToList();

        allBaskets.AddRange(destBaskets);
    }

}